/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-02-03 16:20:26
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import { RightApplyReducer } from '../reducers/RightApplyReducer';
import {
  HIDE_APPLY_DIALOG,
  HIDE_APPROVE_DIALOG,
  SET_APPROVE_LIST,
  SET_ARC_LIST,
  SHOW_APPLY_DIALOG,
  SHOW_APPROVE_DIALOG,
  EDIT_APPROVE_ITEM
} from '../constants';
import { fetchRightApply, fetchRightApplyById } from '@/api/workFlow/apply';
import { startTask } from '@/api/workFlow/task';
import { message } from 'antd';
import { selectItemByKey } from '@/utils/list';
import update from 'immutability-helper';

const initialState = {
  // 借阅单列表
  approveList: [],
  // 借阅单内文件列表
  activeArcList: [],

  // 查看申请弹窗显示状态
  checkDialogVisible: false,

  approveDialog: {}
};

const RightApplyContext = createContext();

const RightApplyContextProvider = (props) => {
  const [state, dispatch] = useReducer(RightApplyReducer, initialState);

  const actions = {
    /**
     * @description: 获取审核任务列表
     * @param {string} assignee
     */
    getApproveList(query) {
      return new Promise((resolve, reject) => {
        fetchRightApply(query)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_APPROVE_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    showApplyRightDialog() {
      dispatch({ type: SHOW_APPLY_DIALOG });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    hideApplyRightDialog(data) {
      dispatch({ type: HIDE_APPLY_DIALOG, data });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    showApproveDialog(id) {
      fetchRightApplyById(id).then((res) => {
        const { data } = res;
        dispatch({ type: SHOW_APPROVE_DIALOG, data });
        // return res;
      });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    hideApproveDialog(data) {
      dispatch({ type: HIDE_APPROVE_DIALOG, data });
    },

    setArcList(id) {
      fetchRightApplyById(id).then((res) => {
        const { data } = res;
        dispatch({ type: SET_ARC_LIST, list: data.archiveItemList || [] });
      });
    },
    startTaskFunc(params) {
      startTask(params).then(() => {
        message.success('申请成功');
        const selectItem = selectItemByKey(params.businessKey, state.approveList, 'applyFormNo');
        const data = update(selectItem, {
          status: {
            $set: selectItem.status + 1
          }
        });
        console.log('start task s', selectItem, data);
        dispatch({ type: EDIT_APPROVE_ITEM, data, id: data.id });
      });
    }
  };

  // watch选中的单位来加载全宗
  useEffect(() => {}, []);

  return (
    <RightApplyContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </RightApplyContext.Provider>
  );
};

export const useRightApplyContext = () => {
  return useContext(RightApplyContext);
};

export { RightApplyContext, RightApplyContextProvider };
